package com.yunjian.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.yunjian.core.dto.SecurityContext;

public class LanguageUtils {

    private static Logger logger = LoggerFactory.getLogger(LanguageUtils.class);

    public static String getValueByKey(String key){
        String language = SecurityContext.getLanguage();
        //logger.info("当前语言环境:{}", language);
        String value = "";
        try {
            if(StringUtils.isEmpty(language)){
                language = "en_US";
            }

            if(language.equals("zh_CN")){
                //properties文件编码为iso-8859-1，读取中文需要转码
                value = SecurityContext.getZh_pro().getProperty(key);
                if(value != null || !StringUtils.isEmpty(value)){
                    value = new String(value.getBytes("ISO-8859-1"), "GBK");
                }else{
                    value = "";
                }
            }else{
                value = SecurityContext.getEn_pro().getProperty(key);
                if(value == null || StringUtils.isEmpty(value)){
                    value = "";
                }
            }
            return value;
        } catch (Exception e) {
            logger.info("获取内容失败", e);
            return value;
        }
    }
}
